package com.appspot.finajjarane.web.mvc.controllers;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.appspot.finajjarane.framework.generic.Utils;
import com.appspot.finajjarane.framework.service.IToolService;

@Controller
@RequestMapping("/tools")
public class Tools {

	@Autowired
	IToolService iToolService;

	@RequestMapping("/{title}/{id}")
	public ModelAndView getTool(@PathVariable("id") Long id){
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			data.put("tool", iToolService.getTool(id));
		} catch (Exception e) {
			data.put("tools", null);
		}
		data.put("menuItem", "tools");
		data.put("lang", Utils.getLanguage());

		return new ModelAndView("tools/tool", data);
	}

	@RequestMapping("/")
	public ModelAndView getToolsList(@PathVariable("page") int page){
		Map<String, Object> data = new HashMap<String, Object>();
		page = Math.max(page, 1);

		try {
			data.put("tools", iToolService.getToolsAll());
		} catch (Exception e) {
			data.put("tools", null);
		}
		data.put("menuItem", "tools");
		data.put("lang", Utils.getLanguage());

		return new ModelAndView("tools/list", data);
	}

}
